package com.szwistar.common;

import com.szwistar.common.datastruct.Utils;

//参考 HTTP 协议错误码：
//400 - 请求无效
//401 - 未授权
//(402, "Payment Required");
//403 - 禁止访问
//404 - 无法找到文件
//405 - 资源被禁止
//406 - 无法接受
//407 - 要求代理身份验证
//410 - 永远不可用
//412 - 先决条件失败
//414 - 请求 - URI 太长
//500 - 内部服务器错误
//501 - 未实现
//502 - 网关错误

//(400, "Bad Request");
//(401, "Unauthorized");
//(402, "Payment Required");
//(403, "Forbidden");
//(404, "Not Found");
//(405, "Method Not Allowed");
//(406, "Not Acceptable");
//(407, "Proxy Authentication Required");
//(408, "Request Timeout");
//(409, "Conflict");
//(410, "Gone");
//(411, "Length Required");
//(412, "Precondition Failed");
//(413, "Payload Too Large");
//(414, "URI Too Long");
//(415, "Unsupported Media Type");
//(416, "Range Not Satisfiable");
//(417, "Expectation Failed");
//(426, "Upgrade Required");
//(500, "Internal Server Error");
//(501, "Not Implemented");
//(502, "Bad Gateway");
//(503, "Service Unavailable");
//(504, "Gateway Timeout");
//(505, "HTTP Version Not Supported");

//参数 nonos 错误码：
//OS_OK                     0    /*!< esp_err_t value indicating success (no error) */
//OS_FAIL                  -1    /*!< Generic esp_err_t code indicating failure */
//OS_ERR_NO_MEM            -2    /*!< Out of memory */
//OS_ERR_INVALID_ARG       -3    /*!< Invalid argument */
//OS_ERR_INVALID_STATE     -4    /*!< Invalid state */
//OS_ERR_INVALID_SIZE      -5    /*!< Invalid size */
//OS_ERR_NOT_FOUND         -6    /*!< Requested resource not found */
//OS_ERR_NOT_SUPPORTED     -7    /*!< Operation or feature not supported */
//OS_ERR_TIMEOUT           -8    /*!< Operation timed out */
//OS_ERR_INVALID_RESPONSE  -9    /*!< Received response was invalid */
//OS_ERR_INVALID_CRC       -10   /*!< CRC or checksum was invalid */
//OS_ERR_INVALID_VERSION   -11   /*!< Version was invalid */
//OS_ERR_INVALID_MAC       -12   /*!< MAC address was invalid */
//OS_ERR_INVALID_IF_TYPE   -13   /*!< Interface type was invalid */
//OS_ERR_INVALID_SRC_ADDR  -14   /*!< Source address was invalid */
//OS_ERR_INVALID_DST_ADDR  -15   /*!< Destination address was invalid */
//OS_ERR_INVALID_FRAME_LEN -16   /*!< Invalid frame length */
//OS_ERR_INVALID_PARAM_LEN -17   /*!< Invalid parameter length */
//OS_ERR_BUFFER_OVERFLOW   -18   /*!< Buffer overflow */
//OS_ERR_BUFFER_UNDERFLOW  -19   /*!< Buffer underflow */
//OS_ERR_INVALID_COMMAND   -20   /*!< Invalid command */

public enum ERR {
	// 基本
	OK              (0  , null),
	FAIL            (-1 , null),

	// HTTP 正常状态码
	HTTP_CONTINUE			(100, "Continue"),
	HTTP_SWITCH				(101, "Switching Protocols"),
	HTTP_OK					(200, "OK"),
	HTTP_CREATED			(201, "Created"),
	HTTP_ACCEPTED			(202, "Accepted"),
	HTTP_NON_AUTH_INFO		(203, "Non-Authoritative Information"),
	HTTP_NO_CONTENT			(204, "No Content"),
	HTTP_RESET_CONTENT		(205, "Reset Content"),
	HTTP_PARTIAL_CONTENT	(206, "Partial Content"),

	// HTTP 跳转码
	HTTP_MULTI_CHOICES		(300, "Multiple Choices"),
	HTTP_MOVED_PERMANENTLY	(301, "Moved Permanently"),
	HTTP_TEMPORARY_REDIRECT	(302, "Temporary Redirect"),
	HTTP_SEE_OTHER			(303, "See Other"),
	HTTP_NOT_MODIFIED		(304, "Not Modified"),
	HTTP_USE_PROXY			(305, "Use Proxy"),
	HTTP_TEMPORARY_REDIRECT2(307, "Temporary Redirect"),

	// 用户原因导致的错误
	PARAMS          (400, "参数错误"),
	NO_AUTH         (401, "无授权信息"),
	AUTH_FAIL       (402, "授权验证失败"),
	FORBIDDEN       (403, "禁止访问"),
	NOT_FOUND       (404, "无法找到资源"),
	DISABLED        (405, "资源未使能"),
	NOT_SUPPORT     (406, "不支持的操作"),
	TOO_FEW_PARAMS  (407, "参数不足"),
	TOO_MUCH_PARAMS (408, "参数过多"),
	SIZE            (409, "错误的大小"),
	TYPE            (410, "错误的类型"),
	NAME            (411, "错误的名称"),
	STATE           (412, "错误的状态"),
	COMMAND         (413, "错误的命令"),
	CRC             (414, "错误的校验码"),
	VERSION         (415, "错误的版本"),
	AUTH_EXPIRED    (416, "授权过期"),
	ALREADY_EXISTS  (417, "对象已经存在"),
	BAD_PARAM       (418, "参数 '%s' 错误: %s"),
	OFFLINE         (419, "不在线"),
	CONFLICT        (420, "出现冲突"),
	OCCUPIED        (421, "已被占用"),

	// 系统内部错误
	SYS             (500, "系统内部错误"),
	UNIMPLEMENTED   (501, "未实现"),
	BAD_GATEWAY     (502, "网关错误"),
	TIMEOUT         (503, "内部操作超时"),
	OVERFLOW        (504, "缓冲区或数组向上溢出"),
	UNDERFLOW       (505, "缓冲区或数组向下溢出"),
	NO_MEM          (506, "内存不足"),
	NO_DISK         (507, "磁盘空间不足"),
	DB_FAIL         (508, "数据库无法访问"),
	DB_QUERY        (509, "数据库查询失败"),
	DB_INERT        (510, "数据库插入失败"),
	DB_DELETE       (511, "数据库删除失败"),
	DB_UPDATE       (512, "数据库更新失败"),
	OUT_OF_BOUNDS   (513, "越界"),
	THIRD_SERVICE   (514, "第三方服务失败"),
	FILE_DELETE     (515, "删除文件失败"),
	FILE_READ       (516, "读文件失败"),
	FILE_WRITE      (517, "写文件失败"),
	FILE_SAVE       (518, "保存文件失败"),
	BLE_OFFLINE_CTRL(519, "BLE 设备离线控制");

	private int code;
	private String msg;

	public int code() { return code; }
	public String msg() { return msg; }

	@Override
	public String toString() {
		if(msg == null) { return "[" + name() + "(" + code() + ")]"; }
		return "[" + name() + "(" + code() + ")] " + msg;
	}

	public String format(Object ...args) {
		if(msg == null || Utils.isEmpty(args)) { return toString(); }
		return "[" + name() + "(" + code() + ")] " + String.format(msg, args);
	}

	ERR(int code, String msg) {
		this.code = code;
		this.msg = msg;
	}

	ERR(int code) {
		this(code, null);
	}

	// for test only
	public static void main(String[] args) {
		System.out.println(ERR.OK);
		System.out.println(ERR.FAIL);
		System.out.println(ERR.PARAMS);
		System.out.println(ERR.BAD_PARAM.format("username", "用户名长度不足6字符"));
	}
}
